package com.daxue.configtest.config;

import com.daxue.configtest.repository.remoteconfig.ConfigRepositoryImpl;
import com.daxue.configtest.service.RemoteConfigService;
import com.daxue.configtest.service.impl.RemoteConfigServiceImpl;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author daxue0929
 * @date 2023/3/25
 */

@Configuration
@EnableConfigurationProperties(LocalEnvironmentProperties.class)
@Import(value = {
    DataSourceAutoConfiguration.class,
    JooqAutoConfiguration.class,
    RemoteConfigServiceImpl.class,
    ConfigRepositoryImpl.class,
    DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class
})
public class RemoteConfigConfiguration {

    @Bean
    public LocalEnvironmentRepositoryFactory localEnvironmentRepositoryFactory(
        final RemoteConfigService remoteConfigService
    ) {
        return new LocalEnvironmentRepositoryFactory(remoteConfigService);
    }

    @Bean
    public LocalEnvironmentRepository localEnvironmentProperties(
        LocalEnvironmentRepositoryFactory factory, LocalEnvironmentProperties properties
    ) {
        return factory.build(properties);
    }
}
